import Vue from 'vue'
import App from './App'
import uView from "uview-ui";
import store from '@/components/modelParser/store'
import getCurrentUri from '@/common/js/getCurrentUri.js'
import getBeforePageUrl from '@/common/js/getBeforePageUrl.js'
import getStorage from '@/common/js/getStorage.js'

Vue.use(uView);

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()

var basePath,
	baseUrl,
	picUrl,
	imgBasePath,
	hostName,
	ENV;



basePath = 'http://172.10.1.15:8799'


// uni.setStorageSync('access_token','f932e1eb939ac57b22e83e9825e90758');
// uni.setStorageSync('userType','1');

const ajax = option => {
	/**
	option = { // option配置说明
		url,
		header,
		method,
		params, // params对象的值会作为字符串拼接在URL请求地址中
		needAuth,  // 是否让header里携带token
		contentType,
		noCode, // 不做code判断及相关处理
		noErrorMsg， // 不需要自动用showToast暴露错误信息
		noToLogin: 不登录
		basePointData: 埋点信息
	}
  */
	return new Promise((resolve, reject) => {
		var token = '';
		var params = '';
		if (option.params) {
			params = '?';
			var keyvalPair = '';
			for (var k in option.params) {
				keyvalPair = k + '=' + option.params[k];
				if (params === '?') {
					params += keyvalPair
				} else {
					params += ('&' + keyvalPair)
				}
			}
			if (params === '?') {
				params = ''
			}
		}
		var c_url
		if (/^https?:\/\//.test(option.url)) {
			c_url = option.url
		} else {
			c_url = basePath + option.url
		}
		// 为了配合接口本地代理，开发时请求地址需要去除域名
		// // #ifdef H5
		// if (location.origin.indexOf('127.0.0.1') > -1) {
		// 	c_url = c_url.replace(baseDomain, '')
		// }
		// // #endif
		var header = {
			'Content-Type': option.contentType ||
				'application/x-www-form-urlencoded;charset=utf-8' // ContentType:'application/json;charset=utf-8'
		}
		if (getStorage('access_token')) {
			token = getStorage('access_token')
		} else {
			token = ''
		}
		if (option.needAuth) {
			header.accessToken = token || ''
		}
		header = Object.assign(header, option.headers)
		uni.request({
			url: c_url + params,
			method: option.method || 'POST',
			header,
			data: option.data || {},
			success(res) {
				res = res.data;
				if (option.noCode) {
					resolve(res)
				} else {
					if (res.code && res.code != 200 && res.code != 401 && !option.noErrorMsg && res
						.code != 500 && res.code != 403) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}

					if (res.code == 500 && !option.noErrorMsg) {
						if (res.msg == "操作失败" || res.msg == "参数错误") {
							uni.hideToast();
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					}

					if (res.status && !option.noErrorMsg) {
						uni.showToast({
							title: res.error,
							icon: 'none'
						})
					}
					resolve(res)
				}
			},
			fail(ret) {
				reject(ret)
			}
		})
	})
}
const gopage = option => {
	if (option) {
		uni.navigateTo({
			url: option
		});
	} else {
		uni.showToast({
			title: '建设中',
			duration: 300,
			icon: 'none'
		})
	}
}
const goBack = (delta = 1) => {
	if (delta < 1) {
		delta = 1
	}
	if (getCurrentPages().length <= 1) {
		uni.switchTab({
			url: '/views/indexPage'
		});
	} else {
		uni.navigateBack({
			delta: delta
		})
	}
}

const getUserInfo = option => {
	return new Promise((resolve, reject) => {
		ajax({
			url: "/api/wwwNews/getUserInfo",
			needAuth: true,
			params: {
				userType: uni.getStorageSync("userType"),
				accessToken: uni.getStorageSync('access_token')
			}
		}).then(res => {
			if (res.code == 200) {
				resolve(res)
			} else {
				
			}
		})
	})
}

const previewImg = (url) => {
	//图片预览
	if (typeof url == 'string') {
		url = [url]
	}
	uni.previewImage({
		current: 0,
		urls: url,
	});
}

Vue.prototype.$ajax = ajax;
Vue.prototype.$basePath = basePath;
Vue.prototype.$imgBasePath = imgBasePath;
Vue.prototype.$goBack = goBack;
Vue.prototype.$gopage = gopage;
Vue.prototype.$getUserInfo = getUserInfo;
Vue.prototype.$previewImg = previewImg;
Vue.prototype.$afterTokenError = ()=>{
	console.log('token失效了，触发了$afterTokenError');
}
export {
	ajax
}
